<?php

namespace app\common\model\xilumarket;

use think\Db;
use think\Log;
use think\Model;



class ProductWant extends Model
{
    // 表名
    protected $name = 'xilumarket_product_want';

    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;


    /**
     * @param int $userId
     * @param int $productId
     * @return bool|string
     */
    public static function add($userId, $productId) {
        try {
            $model = static::where('user_id', $userId)
                ->where('product_id', $productId)
                ->find();
            if ($model) {
                 return true;
            } else {
                $model = new static([
                    'user_id' => $userId,
                    'product_id' => $productId
                ]);
                if ($model->save() === false) {

                    //return $model->getError();
                    return false;
                }
            }
            $count = self::where('product_id', $productId)->count();
            Product::where('id', $productId)->update(['want_num' => $count]);
        }catch (\Exception $e) {
            Log::error($e->getMessage()."\n".$e->getTraceAsString());
            return false;
        }
        return true;
    }



}
